/**
 * 管理用户
 */
<template>
    <vp-dialog ref="editDialogRef" :span="1">
        <template #title>
         <vp-label  zh="设置管理员" en="Set Admin" align="left"/>
        </template>
        <el-form label-suffix=":"  :model="formData" :rules="formRules" ref="formRef" label-width="120px">
            <el-row>
                <vp-col :span="1">
                    <el-form-item  prop="username">
                        <template #label><vp-label zh="用户名" en="Account"/></template>
                        <el-input v-model="formData.username" v-trim maxlength="30"></el-input>
                    </el-form-item>
                </vp-col>
                <vp-col :span="1">
                    <el-form-item  prop="realname">
                        <template #label><vp-label zh="姓名" en="Name"/></template>
                        <el-input v-model="formData.realname" v-trim maxlength="100"></el-input>
                    </el-form-item>
                </vp-col>
            </el-row>
        </el-form>
        <template #footer>
            <vp-btn-confirm :loading="saveLoading" @click="handleSave"/>
        </template>
    </vp-dialog>
</template>
<script setup lang="ts" name="addAdminUser">
import { ref } from 'vue';
import request from '/@/utils/request';
import validateUtils from '/@/utils/validateUtils';

    const emits = defineEmits(['formSubmited']);
    const formData = ref({
        username: '',
        realname: '',
    });
    const formRules = {
        username: [
            validateUtils.notNull,
            validateUtils.notChinese,
        ],
        realname: [
            validateUtils.notNull
        ]
    }

    const dprtcode = ref("");
    const saveLoading = ref(false);

    const editDialogRef = ref();
    const formRef = ref();
    const init = ()=>{
        formData.value = {
            username: '',
            realname: '',
        }
    }

    const handleOpen = (_dprtcode: string)=>{
        init();
        dprtcode.value = _dprtcode;
        editDialogRef.value.handleOpen();
    }

    const handleSave = ()=>{
        saveLoading.value = true;
        formRef.value.validate((valid: Boolean)=>{
            if(valid){
                request.post("/system/tenant/adminUser", {
                    department: {
                        dprtcode: dprtcode.value,
                    },
                    user: formData.value,
                }).then(()=>{
                    emits("formSubmited");
                    editDialogRef.value.handleClose();
                }).catch(()=>{
                }).finally(()=>{
                    saveLoading.value = false;
                })
            }else{
                saveLoading.value = false;
            }
        })
    }

    defineExpose({
        handleOpen,
    })

</script>
